"""
项目数据模型
"""
from datetime import datetime
from typing import List, Optional
from sqlalchemy import (
    Column, Integer, String, Boolean, DateTime, Text, 
    JSON, ForeignKey, Enum as SQLEnum
)
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
import enum

from app.core.database import Base


class ProjectStatus(str, enum.Enum):
    """项目状态枚举"""
    ACTIVE = "active"
    ARCHIVED = "archived"
    DELETED = "deleted"


class ScenarioType(str, enum.Enum):
    """场景类型枚举"""
    SMART_LIGHTING = "smart_lighting"
    MULTI_FUNCTION_POLE = "multi_function_pole"
    SMART_PARKING = "smart_parking"
    SMART_AGRICULTURE = "smart_agriculture"
    ENERGY_MANAGEMENT = "energy_management"
    TRAFFIC_MANAGEMENT = "traffic_management"
    ENVIRONMENTAL_MONITORING = "environmental_monitoring"


class Project(Base):
    """项目模型"""
    __tablename__ = "projects"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, index=True)
    description = Column(Text, nullable=True)
    scenario_type = Column(SQLEnum(ScenarioType), nullable=False)
    status = Column(SQLEnum(ProjectStatus), default=ProjectStatus.ACTIVE, nullable=False)
    
    # 项目配置
    config = Column(JSON, default=dict, nullable=False)  # 项目特定配置
    tags = Column(JSON, default=list, nullable=False)  # 项目标签
    
    # 权限和所有权
    owner_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    is_public = Column(Boolean, default=False, nullable=False)
    
    # 统计信息
    training_jobs_count = Column(Integer, default=0, nullable=False)
    models_count = Column(Integer, default=0, nullable=False)
    data_sources_count = Column(Integer, default=0, nullable=False)
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
    
    # 关系
    owner = relationship("User", back_populates="projects")
    data_sources = relationship("DataSource", back_populates="project", cascade="all, delete-orphan")
    training_jobs = relationship("TrainingJob", back_populates="project", cascade="all, delete-orphan")
    models = relationship("Model", back_populates="project", cascade="all, delete-orphan")
    
    def __repr__(self):
        return f"<Project(id={self.id}, name='{self.name}', scenario='{self.scenario_type}')>"